package Logic;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AppointmentDB;
import Database.AuditLogDB;
import Model.Appointment;

/**
 * Servlet implementation class AppointmentNew_BLL
 */
@WebServlet("/AppointmentNew_BLL")
public class AppointmentNew_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AppointmentNew_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		if (request.getParameter("apt_name") != ""
				&& request.getParameter("apt_responsibility") != "") {
			if (!AppointmentDB.nameExists(request.getParameter("apt_name"))) {
				Appointment appt = new Appointment();
				appt.setId(Integer.toString((AppointmentDB.getLastNumber() + 1)));
				appt.setName(request.getParameter("apt_name"));
				appt.setResponsibilities(request
						.getParameter("apt_responsibility"));

				AppointmentDB.createAppointment(appt);

				AuditLogDB.insertLog("create appointment", (String) request
						.getSession().getAttribute("name"));

				response.sendRedirect("appttype_newappt.jsp?error=-1");
			} else {
				response.sendRedirect("appttype_newappt.jsp?error=2");
			}
		} else {
			response.sendRedirect("appttype_newappt.jsp?error=1");
		}
	}

}
